package ui;

import javax.microedition.lcdui.*;
import javax.microedition.media.protocol.DataSource;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class Gui extends MIDlet implements CommandListener {
	private Form mForm;
	private Display mDisplay;
	private TextField txtId,txtName,txtFoodServe,txtLocation;
	private ChoiceGroup  txtEstablishment; 
	
	private Command mExitCommand,mNextCommand;
	private Alert mAlert;
	private String[] TypeOfEstablishment = {"Cafe", "Bar" , "Restaurant" };
	public Gui(){
		
		
		mForm = new Form("Infomation of the establishment");
		txtId = new TextField("ID","", 15, TextField.ANY);
		txtName = new TextField("Name","", 15, TextField.ANY);
		
		txtEstablishment = new ChoiceGroup("Type of Establishment", Choice.POPUP , TypeOfEstablishment ,null);
		txtEstablishment.setLayout(Item.LAYOUT_EXPAND);
        
		txtFoodServe = new TextField("Type of food served","", 15, TextField.ANY);
		txtLocation = new TextField("Location of the Establishment","", 15, TextField.ANY);
		
		mExitCommand = new Command("Exit", Command.EXIT, 0);
		mNextCommand = new Command("Next",Command.SCREEN,0);
		
		mForm.addCommand(mExitCommand);
		mForm.addCommand(mNextCommand);
		mForm.setCommandListener(this);
		
		mForm.append(txtId);
		mForm.append(txtName);
		mForm.append(txtEstablishment);
		mForm.append(txtFoodServe);
		mForm.append(txtLocation);
		
		
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		mDisplay = Display.getDisplay(this);
		mDisplay.setCurrent(mForm);
	}

	public void commandAction(Command c, Displayable d) {
			if(c== mExitCommand){
				try {
					destroyApp(false);
					notifyDestroyed();
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}
			}
			if(c == mNextCommand){
	            String strEstablishmentDetails = " Infomation of the establishment :\n ID : " + txtId.getString() 
	            																+ "\n Name : " + txtName.getString()
	            																+ "\n Type of Establishment : " + txtEstablishment.getString(txtEstablishment.getSelectedIndex())
	            																+ "\n Type of food served : " + txtFoodServe.getString()
	            																+ "\n Location of the Establishment : " + txtLocation.getString();
	            mAlert = new Alert("",strEstablishmentDetails,null,AlertType.CONFIRMATION);
	            mAlert.addCommand(new Command("Back", Command.BACK, 0));
	            mAlert.addCommand(new Command("Save", Command.SCREEN, 1));        
	            mAlert.setTimeout(Alert.FOREVER);
	            mAlert.setCommandListener(this);

	            mDisplay.setCurrent(mAlert);
			}	
			if(c.getLabel().equals("Save") && d == mAlert){
				try {
					RecordStore rs = RecordStore.openRecordStore("Details", true);
					String info = txtId.getString()
								+";" + txtName.getString()
								+";" + txtEstablishment.getString(txtEstablishment.getSelectedIndex())
								+";" + txtFoodServe.getString()
								+";" + txtLocation.getString();
					byte[] infoBuffer = info.getBytes();
					rs.addRecord(infoBuffer, 0, infoBuffer.length);
					rs.closeRecordStore();
					mAlert.setString(" ** Details saved ** ");
					txtId = new TextField("ID","", 15, TextField.ANY);
					txtName = new TextField("Name","", 15, TextField.ANY);
					
					txtEstablishment = new ChoiceGroup("Type of Establishment", Choice.POPUP , TypeOfEstablishment ,null);
					txtEstablishment.setLayout(Item.LAYOUT_EXPAND);
			        
					txtFoodServe = new TextField("Type of food served","", 15, TextField.ANY);
					txtLocation = new TextField("Location of the Establishment","", 15, TextField.ANY);
				}
				catch (Exception e) { // an error occurred saving
					mAlert.setString("Couldn't save details");
	                System.err.println("Error saving to database");
	                e.printStackTrace();
	            } 

				
			}
		}

}
